package sunwul.mall.commoncore.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author sunwul
 * @date 2024/12/26 16:44
 * @description Spring Security安全框架认识的安全用户对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SecurityUser implements UserDetails {

    /**
     * userId
     */
    private Long userId;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 状态  0：禁用   1：正常
     */
    private Integer status;
    /**
     * 用户所在的商城Id
     */
    private Long shopId;

    /**
     *  用于区分不同系统
     */
    private String loginType;

    /**
     * 权限集合
     */
    private Set<String> perms = new HashSet<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.loginType + "-" + this.userId;
    }

    // 过期
    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    // 锁定
    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    // 禁用
    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }

    public Set<String> getPerms() {
        HashSet<String> finalPermsSet = new HashSet<>();
        // 遍历用户权限集合
        perms.forEach(perm -> {
            // 判断是否含, 包含需要分割
            if (perm.contains(",")) {
                String[] realPerm = perm.split(",");
                finalPermsSet.addAll(Arrays.asList(realPerm));
            } else {
                finalPermsSet.add(perm);
            }
        });
        return finalPermsSet;
    }

    /**
     * =================手动处理字段序列化反序列化的问题  start===================
     * 登录成功后存放redis的为Authentication.getPrincipal,有些字段在此类不存在, 从redis反序列化时会因为字段缺少报错,
     * 此处添加上对应的getter/setter方法以解决报错
     */
    public boolean getEnabled() {
        return this.status == 1;
    }

    public void setEnabled(boolean enabled) {
        if (enabled) {
            this.status = 1;
        } else {
            this.status = 0;
        }
    }

    public boolean getAccountNonExpired() {
        return this.status == 1;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        if (accountNonExpired) {
            this.status = 1;
        } else {
            this.status = 0;
        }
    }

    public boolean getAccountNonLocked() {
        return this.status == 1;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        if (accountNonLocked) {
            this.status = 1;
        } else {
            this.status = 0;
        }
    }

    public boolean getCredentialsNonExpired() {
        return this.status == 1;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        if (credentialsNonExpired) {
            this.status = 1;
        } else {
            this.status = 0;
        }
    }

    /*
      =================手动处理序列化反序列化  end===================
     */
}
